package com.ddone.demo.converter;

import com.ddone.demo.bean.Address;
import org.springframework.core.convert.converter.Converter;

import java.util.Optional;

/**
 * @author ddone
 * @date 2024/3/23-17:42
 */
public class AddressConverter implements Converter<String, Address> {
    @Override
    public Address convert(String s) {
        return Optional.of(s.split(","))
                .filter(arr -> arr.length >= 3)
                .map(arr -> Address.builder()
                        .province(arr[0])
                        .city(arr[1])
                        .area(arr[2])
                        .build())
                .orElse(new Address());
    }

}
